//
//  THAddCommentPresentViewController.m
//  TreeHole
//
//  Created by stoneobs on 2021/1/6.
//  Copyright © 2021 stoneobs. All rights reserved.
//

#import "THAddCommentPresentViewController.h"
#import "STTools_TextView.h"
@interface THAddCommentPresentViewController ()<UITextViewDelegate>
@property(nonatomic, strong) STButton                     *stoneobs_ui_clearButton;
@property(nonatomic, strong) UIView                     *stoneobs_ui_customView;

@property(nonatomic, strong) STTools_TextView                     *textView;/**<  */
@end

@implementation THAddCommentPresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self stoneobs_ui_configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self stoneobs_fun_show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    self.view.backgroundColor = UIColor.clearColor;
    __weak typeof(self) weakSelf =  self;
    self.stoneobs_ui_clearButton = [[STButton alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME
                                                             title:nil
                                                        titleColor:nil
                                                         titleFont:0
                                                      cornerRadius:0
                                                   backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                                   backgroundImage:nil
                                                             image:nil];
    [weakSelf.stoneobs_ui_clearButton setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_fun_dismiss];
    }];
    [self.view addSubview:_stoneobs_ui_clearButton];
    
    self.stoneobs_ui_customView = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    self.stoneobs_ui_customView.centerX = UISCREEN_WIDTH / 2;
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    self.stoneobs_ui_customView.backgroundColor = [UIColor clearColor];
    self.stoneobs_ui_customView.layer.cornerRadius = 0;
    self.stoneobs_ui_customView.clipsToBounds = YES;
    [self.view addSubview:self.stoneobs_ui_customView];
    
    [self stoneobs_fun_st_autoAdjustAllResponder];
    
    STTools_TextView * textView = [[STTools_TextView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 150)];
    textView.placeholder = @"请输入评价";
    textView.layer.cornerRadius = 10;
    textView.clipsToBounds = YES;
    [self.stoneobs_ui_customView addSubview:textView];
    self.textView = textView;
    textView.returnKeyType = UIReturnKeySend;
    textView.delegate = self;
    self.stoneobs_ui_customView.height = textView.bottom;
    
    if (@available(iOS 11.0, *)) {
        self.stoneobs_ui_customView.height = self.stoneobs_ui_customView.height  + UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
    } else {
        // Fallback on earlier versions
    }
    
    
    
    
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    
    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        if (self.didFillComment) {
            self.didFillComment(self.textView.text);
        }
        [self stoneobs_fun_dismissWithHandle:^{
            
        }];
    }
    return YES;
}
- (void)stoneobs_fun_show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
        self.stoneobs_ui_customView.bottom = UISCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)stoneobs_fun_dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)stoneobs_fun_dismissWithHandle:(void(^)(void))handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}

@end
